package com.wlc.controller;

import com.wlc.pojo.Resignation;
import com.wlc.pojo.ResignationStats;
import com.wlc.pojo.Result;
import com.wlc.service.ResignationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resignation")
@Slf4j
public class ResignationController {

    @Autowired
    private ResignationService resignationService;

    @GetMapping("/{id}")
    public Result<Resignation> getById(@PathVariable Long id) {
        log.info("根据ID查询辞职申请: {}", id);
        try {
            Resignation resignation = resignationService.findById(id);
            return Result.success(resignation);
        } catch (Exception e) {
            log.error("查询辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<List<Resignation>> list(Resignation resignation) {
        log.info("查询辞职申请列表");
        try {
            List<Resignation> list = resignationService.findList(resignation);
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询辞职申请列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    public Result<String> save(@RequestBody Resignation resignation) {
        log.info("新增辞职申请: employeeId={}", resignation.getEmployeeId());
        try {
            resignationService.save(resignation);
            return Result.success("提交辞职申请成功");
        } catch (Exception e) {
            log.error("新增辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    public Result<String> update(@RequestBody Resignation resignation) {
        log.info("更新辞职申请: {}", resignation.getId());
        try {
            resignationService.update(resignation);
            return Result.success("更新辞职申请成功");
        } catch (Exception e) {
            log.error("更新辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除辞职申请: {}", id);
        try {
            resignationService.deleteById(id);
            return Result.success("删除辞职申请成功");
        } catch (Exception e) {
            log.error("删除辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/{id}/approve")
    public Result<String> approve(@PathVariable Long id,
                                  @RequestParam Long approverId,
                                  @RequestParam(required = false) String approvalOpinion) {
        log.info("批准辞职申请: id={}, approverId={}", id, approverId);
        try {
            resignationService.approveResignation(id, approverId, approvalOpinion);
            return Result.success("批准辞职申请成功");
        } catch (Exception e) {
            log.error("批准辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/{id}/reject")
    public Result<String> reject(@PathVariable Long id,
                                 @RequestParam Long approverId,
                                 @RequestParam(required = false) String approvalOpinion) {
        log.info("拒绝辞职申请: id={}, approverId={}", id, approverId);
        try {
            resignationService.rejectResignation(id, approverId, approvalOpinion);
            return Result.success("拒绝辞职申请成功");
        } catch (Exception e) {
            log.error("拒绝辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/{id}/cancel")
    public Result<String> cancel(@PathVariable Long id) {
        log.info("取消辞职申请: {}", id);
        try {
            resignationService.cancelResignation(id);
            return Result.success("取消辞职申请成功");
        } catch (Exception e) {
            log.error("取消辞职申请失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/stats/dept")
    public Result<List<ResignationStats>> getDeptStats(@RequestParam(required = false) String year) {
        log.info("获取部门辞职统计: year={}", year);
        try {
            List<ResignationStats> stats = resignationService.getDeptResignationStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取部门辞职统计失败: {}", e.getMessage());
            return Result.error("获取部门辞职统计失败");
        }
    }

    @GetMapping("/stats/monthly-trend")
    public Result<List<ResignationStats>> getMonthlyTrend(@RequestParam(required = false) String year) {
        log.info("获取月度辞职趋势: year={}", year);
        try {
            List<ResignationStats> trend = resignationService.getMonthlyResignationTrend(year);
            return Result.success(trend);
        } catch (Exception e) {
            log.error("获取月度辞职趋势失败: {}", e.getMessage());
            return Result.error("获取月度辞职趋势失败");
        }
    }

    @GetMapping("/stats/reason")
    public Result<List<ResignationStats>> getReasonStats(@RequestParam(required = false) String year) {
        log.info("获取辞职原因统计: year={}", year);
        try {
            List<ResignationStats> stats = resignationService.getResignReasonStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取辞职原因统计失败: {}", e.getMessage());
            return Result.error("获取辞职原因统计失败");
        }
    }

    @GetMapping("/stats/summary")
    public Result<ResignationStats> getSummary(@RequestParam(required = false) String year) {
        log.info("获取辞职汇总统计: year={}", year);
        try {
            ResignationStats summary = resignationService.getResignationSummary(year);
            return Result.success(summary);
        } catch (Exception e) {
            log.error("获取辞职汇总统计失败: {}", e.getMessage());
            return Result.error("获取辞职汇总统计失败");
        }
    }

    @GetMapping("/stats/overview")
    public Result<Object> getOverview(@RequestParam(required = false) String year) {
        log.info("获取辞职统计总览: year={}", year);
        try {
            Object overview = resignationService.getResignationOverview(year);
            return Result.success(overview);
        } catch (Exception e) {
            log.error("获取辞职统计总览失败: {}", e.getMessage());
            return Result.error("获取辞职统计总览失败");
        }
    }
}